# -*- coding=utf-8 -*-

class RecordIP :
    def __init__(self, filename):
        self.__filename = filename
        self.cache = dict()

    def init(self):
        try:
            f = open( self.__filename, 'r' )
            lines = f.readlines()
            f.close()

        except IOError, e:
            pass
        else:
            for line in lines:
                seg_idx = line.find('=')
                if seg_idx is not None :
                    v = line[seg_idx+1:]
                    if v[-1] == '\n':
                        v = v[:-1]
                    self.cache[line[:seg_idx]] = v
                    # print 'read: ', line[:seg_idx], '=', v

    def export(self):
        try:
            f = open( self.__filename, 'w' )
            for k, v in self.cache.items():
                l = '%s=%s\n'%(k, v)
                f.write( l )
            f.flush()
            f.close()
        except IOError, e:
            pass
        else:
            pass

    def record(self, key, value):
        self.cache[key]=value

